
<?php
include_once('./wordfilter.php.inc');
class Document {
	var $id; #Document ID
	var $termFreq = array(); #term frequency vector
	var $maxTermFreq; #maximum frequency in term frequency vector
	var $length; #Document length in words
	var $title; #Document title
	var $weights = array();
	var $weightsLSI = array();
	private $label;

	public function importLabeledWords ($text, $label) {
		$this->label = $label;
		$this->importWords($text);
	}

	public function setLabel($label){
		$this->label = $label;
	}

	public function getLabel(){
		return $this->label;
	}

	public function importWords ($text) {
		$words=Filter::filterText($text);
		foreach($words as $w){
			if(!isset($this->termFreq[$w])){
				$this->termFreq[$w] = 0;
			}
			$this->termFreq[$w]++;
			if ($this->maxTermFreq < $this->termFreq[$w]) {
				$this->maxTermFreq = $this->termFreq[$w];
			}
			$this->length++;
		}
	}
	public function filterTermFrequency($existingTerms){
		$this->maxTermFreq=0;
		$newTermFrequency=array();
		foreach($this->termFreq as $term => $freq){
			if(isset($existingTerms[$term])){
				$newTermFrequency[$term]=$freq;
				if ($this->maxTermFreq < $freq) {
					$this->maxTermFreq = $freq;
				}
			}else{
			//	echo($term." was not recognized\r\n");
				$this->length--;
			}
		}
		$this->termFreq=$newTermFrequency;
	}
}
?>